import axios from "axios";
//引入进度条
import nProgress from "nprogress";
import "nprogress/nprogress.css";
//引入仓库
import store from "@/store";
const requests = axios.create({
  baseURL: "/api",
  timeout: 50000,
});
//请求拦截器
requests.interceptors.request.use((config) => {
  //TODO
  nProgress.start();
  if (store.state.detail.uuid_token) {
    //请求头添加一个字段userTempId
    config.headers.userTempId = store.state.detail.uuid_token;
  }
  if (store.state.user.token) {
    config.headers.token = store.state.user.token;
  }

  return config;
});
//响应拦截器
requests.interceptors.response.use(
  (res) => {
    //进度条结束
    nProgress.done();
    return res.data;
  },
  (error) => {
    return Promise.reject(new Error(error));
  }
);
export default requests;
